<template>
  <el-main>
    <el-button class="list-btn" type="primary" icon="el-icon-plus" @click="createItem" v-if="btnShow">创建</el-button>
    <el-table :data="tableData" :row-class-name="tableRowClassName" v-if="listShow">
      <el-table-column prop="num" label="序号"></el-table-column>
      <el-table-column prop="name" label="角色名"></el-table-column>
      <el-table-column prop="id" label="操作">
        <template slot-scope="scope">
          <el-button
            class="list-btn"
            type="success"
            @click="editItem(scope.$index,scope.row)"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button 
            class="list-btn"
            type="danger"
            @click="deleteItem(scope.$index,scope.row)"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-form ref="form" :model="form" label-width="80px" v-if="editShow">
      <el-form-item label="角色名">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
   
     <el-form-item label="所属用户">
        <el-checkbox-group v-model="form.users" v-if="newShow==false">
          <el-checkbox
            v-for="user in edit.users"
            :key="user.id"
            :label="user"
            :checked="check(edit.role.users, user)"
            name="type"
          >{{user.username}}</el-checkbox>
        </el-checkbox-group>
        <el-checkbox-group v-model="form.users" v-else>
          <el-checkbox
            v-for="user in edit.users"
            :key="user.id"
            :label="user"
            name="type"
          >{{user.username}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
       
      <el-form-item>
        <el-button type="primary" @click="onSubmit" v-if="newShow==false">确定</el-button>
        <el-button type="primary" @click="add" v-else>确定</el-button>
        <el-button @click="back">取消</el-button>
      </el-form-item>
    </el-form>
  


  </el-main>
</template>

<script type="module">
var roles = [];
var formData = [];
var editData = [];
var delFlag = false;
module.exports = {
  data: function() {
    return {
      tableData: roles,
      editShow: false,
      listShow: true,
      newShow: false,
      btnShow: true,
      edit: editData,
      emptyForm: {
        id: "",
        name: "",
        users: [],
      },
      form: {
        id: "",
        name: "",
        users: [],
      }
    };
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      row.index = rowIndex;
    },
    gettable() {
      $.ajax({
        url: "/listAllRoles",
        type: "get",
        dataType: "json",
        async: false,
        success(data) {
          console.log(data.roles);
          for (let i in data.roles) {
            roles[i] = {
              num: i,
              name: data.roles[i].name,
              id: data.roles[i].id,
            };
          }
        }
      });
    },
    deleteItem(item, index) {
      let id = '{"id": ' + index.id + "}";
      $.ajax({
        url: "/deleteRole",
        type: "post",
        data: JSON.parse(id),
        async: false,
        success(data) {
          if(data) {
            delFlag = true;
          } else {
            delFlag = false;
          } 
        }
      });
      if(delFlag) {
        this.$message({
          message: '恭喜你，删除成功',
          type: 'success'
        });
        setTimeout(function(){
          window.location.href = "/index?page=8";
        }, 1500);
      } else {
        this.$message.error('错误窝，删除失败');
      }
    },
    editItem(item, index) {
      this.newShow = false;
      this.listShow = false;
      this.editShow = true;
      this.btnShow = false;
      let id = '{"id": ' + index.id + "}";
      $.ajax({
        url: "/editRole",
        type: "get",
        dataType: "json",
        async: false,
        data: JSON.parse(id),
        success(data) {
          console.log(data);
          editData = {
            role: data.role,
            users: data.users,
          };
          console.log(editData);
          formData = {
            id: data.role.id,
            name: data.role.name,
            users: [],
          };
          console.log(formData.role);
        }
      });
      this.edit = editData;
      this.form = formData;
    },
    check(objs, obj) {
      for (let i in objs) {
        if (objs[i].id == obj.id) return true;
      }
      return false;
    },
    onSubmit() {
      console.log(JSON.stringify(this.form));
      $.ajax({
        url: "/updateRole",
        type: "post",
        contentType: "application/json",
        async: false,
        data: JSON.stringify(this.form),
        success(data) {
          window.location.href = "/index?page=8";
        }
      });
    },
    back() {
      this.listShow = true;
      this.editShow = false;
      this.btnShow = true;
      this.emptyForm = {
        id: "",
        name: "",
        users: [],
      };
    },
    createItem() {
      this.newShow = true;
      this.listShow = false;
      this.editShow = true;
      this.btnShow = false;
      $.ajax({
        url: "/newRole",
        type: "get",
        dataType: "json",
        async: false,
        success(data) {
          editData = {
            users: data.users,
          };
          //console.log(editData);
        }
      });
      this.edit = editData;
      this.form = this.emptyForm;
    },
    add() {
      console.log(JSON.stringify(this.form));
      $.ajax({
        url: "/addRole",
        type: "post",
        contentType: "application/json",
        async: false,
        data: JSON.stringify(this.form),
        success(data) {
          window.location.href = "/index?page=8";
        }
      });
    }
  },
  mounted: function() {
    // console.log("3");
    this.gettable();
    // console.log("4");
  }
};
</script>